package com.guli.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.commonutils.R;
import com.guli.eduService.entity.EduCourse;
import com.guli.eduService.entity.EduTeacher;
import com.guli.eduService.entity.vo.CourseInfoVo;
import com.guli.eduService.entity.vo.CoursePublishVo;
import com.guli.eduService.entity.vo.CourseQuery;
import com.guli.eduService.service.EduCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author LMZovo
 * @since 2022-02-15
 */
@Api(tags = "课程表-存储课程基本信息")
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "课程列表")
    @GetMapping
    public R getCourseList() {
        List<EduCourse> list = courseService.list(null);
        return R.ok().data("list", list);
    }


    @ApiOperation(value = "添加课程的基本信息")
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        //返回添加之后的课程id，为后面使用
        String id = courseService.saveCourseInfo(courseInfoVo);
        return R.ok().data("courseId", id);
    }

    @ApiOperation(value = "根据课程id查询课程的基本信息")
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable("courseId") String courseId) {
        CourseInfoVo courseInfoVo = courseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoVo", courseInfoVo);
    }

    @ApiOperation(value = "修改课程信息")
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo) {
        courseService.updateCourseInfo(courseInfoVo);
        return R.ok();
    }

    @ApiOperation(value = "根据课程id查询课程确认信息")
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable("id") String id) {
        CoursePublishVo publishVo = courseService.publishCourseInfo(id);
        return R.ok().data("publishCourse", publishVo);
    }

    @ApiOperation(value = "课程最终发布") //修改课程的状态
    @PostMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable("id") String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal"); //设置课程的发布状态
        courseService.updateById(eduCourse);
        return R.ok();
    }

    @ApiOperation(value = "分页")
    @GetMapping("pageCourse/{current}/{limit}")
    public R pageCourse(@PathVariable long current,
                        @PathVariable long limit) {
        Page<EduCourse> page = new Page<>(current, limit);
        courseService.page(page, null);

        long total = page.getTotal();//总记录数
        List<EduCourse> records = page.getRecords(); //数据list集合
        Map<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);

        return R.ok().data(map);
    }

    @ApiOperation(value = "条件查询带分页")
    @PostMapping("pageCourseCondition/{current}/{limit}")
    public R pageCourseCondition(@PathVariable long current, @PathVariable long limit,
                                 @RequestBody(required = false) CourseQuery courseQuery) {
        //创建page对象
        Page<EduCourse> page = new Page<>(current, limit);
        //构建查询条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断是否为空
        String title = courseQuery.getName();
        String status = courseQuery.getStatus();
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        courseService.page(page, wrapper);
        long total = page.getTotal();
        List<EduCourse> records = page.getRecords(); //总记录数
        return R.ok().data("total", total).data("rows", records); //数据list集合
    }

    @ApiOperation("删除课程")
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable("courseId") String courseId) {
        courseService.removeCourse(courseId);
        return R.ok();
    }


}